define(["jquery", "easy-admin"], function ($, ea) {

    var init = {
        table_elem     : '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url      : 'user.user/index',
        add_url        : 'user.user/add',
        edit_url       : 'user.user/edit',
        set_field      : 'user.user/setField'
    };

    //得到各种内置组件
    var layer   = layui.layer //弹层
        , table = layui.table;

    return {
        index        : function () {
            ea.table.render({
                init   : init,
                cols   : [
                    [
                        {type: 'checkbox'},
                        {field: 'uid', title: 'ID', search: false},
                        {field: 'nickname', title: '昵称', search: true},
                        {field: 'phone', title: '手机号', search: true},
                        {field: 'avatar', title: '用户头像', search: false, templet: ea.table.image},
                        {field: 'sex', search: 'false', selectList: {"1": "男", "2": "女"}, title: '性别'},
                        {field: 'surplus_price', title: '用户余额', search: false},
                        {field: 'commission_price', title: '佣金金额', search: false},
                        {field: 'grow_up', title: '成长值', search: false},
                        {field: 'integral', title: '积分', search: false},
                        {field: 'member_id', search: 'true', title: '会员等级', hide: true},
                        {field: 'member_name', search: 'false', title: '会员等级'},
                        {
                            field     : 'is_disable',
                            search    : 'select',
                            selectList: {"1": "正常", "0": "冻结"},
                            title     : '状态',
                            templet   : ea.table.switch
                        },
                        {field: 'pay_count', title: '购买次数', search: false},
                        {field: 'create_time', title: '注册时间', search: 'range'},
                        {
                            fixed: 'right', width: 200, title: '操作', templet: ea.table.tool, operat: [
                                'edit',
                                [
                                    {
                                        text: '用户详情',
                                        url : 'user.UserManage/index'
                                    }
                                ]
                            ]
                        }
                    ]
                ],
                where  : {"delete_time": 0},
                toolbar: [
                    'refresh',
                    [
                        {
                            text : ' 发送优惠券',
                            class: 'layui-btn layui-btn-primary layui-border-green layui-btn-sm coupon'
                        }
                    ]
                ]
            });
            // 给指定用户发优惠券
            $('.coupon').click(function () {
                var idsArray = [];
                if ($(".layui-form-checked ").length > 0) {
                    $(".layui-table-body .laytable-cell-checkbox ").each(function (i, item) {
                        if ($(item).find(".layui-form-checked ").length > 0) {
                            var id = $(".layui-table-body .layui-table tr[data-index=" + i + "] .laytable-cell-1-0-1").text();
                            idsArray.push(id);
                        }
                    });
                    layer.open({
                        type      : 2,
                        title     : '发送优惠券',
                        area      : ['85%', '70%'],
                        resize    : false,
                        move      : false,
                        anim      : 2,
                        offset    : 'c',
                        shadeClose: false,
                        content   : "/admin/user.User/releaseCoupon?ids=" + idsArray.toString()
                    });
                } else {
                    layer.msg("请选择用户");
                }
                return false;//  阻止跳转
            })
            ea.listen();
        },
        releaseCoupon: function () {
            // 发送优惠券
            var init1 = {
                table_elem     : '#currentTable',
                table_render_id: 'currentTableRenderId',
                index_url      : 'coupon.coupon/index'
            };
            ea.table.render({
                init   : init1,
                cols   : [
                    [
                        // {type: 'checkbox'},
                        {field: 'id', title: 'ID', search: false},
                        {field: 'coupon_title', title: '优惠券名称', search: true},
                        {field: 'coupon_price', title: '优惠券面值', search: false},
                        {field: 'coupon_type', search: 'false', selectList: ["满减"], title: '优惠券类型'},
                        {
                            fixed: 'right', width: 100, title: '操作', templet: function (d) {
                                return '<a class="layui-table-text send" id="' + d.id + '" data-title="发送 ID: ' + d.id + '">发送</a>';
                            }
                        }
                    ]
                ],
                where  : {"delete_time": 0},
                toolbar: ['refresh']
            });
            // 发布
            $(document).on('click', '.send', function () {
                var that = this;
                var id   = $('.send').attr('id');
                var url  = '/user.coupon.UserCoupon/releaseCoupon?ids=' + ids + '&id=' + id;
                $.get(url, function (data, status) {
                    if (status === 'success') {
                        that.tip('发布成功');
                    } else {
                        that.tip('发布失败');
                    }
                });
            });
            ea.listen();
        },
        edit         : function () {
            ea.listen();
        },
        tip          : function (string) {
            //显示自动关闭倒计秒数
            layer.alert(string, {
                time     : 5 * 1000
                , success: function (layero, index) {
                    var timeNum = this.time / 1000, setText = function (start) {
                        layer.title((start ? timeNum : --timeNum) + ' 秒后关闭', index);
                    };
                    setText(!0);
                    this.timer = setInterval(setText, 1000);
                    if (timeNum <= 0) clearInterval(this.timer);
                }
                , end    : function () {
                    clearInterval(this.timer);
                }
            });
        }
    };
});